package com.wolfalpha.utron.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.view.city.CityPicker;

/**
 * 自定义的对话框
 *
 * @author
 */
public class CityAlertDialog {
    private Context context;
    private android.app.AlertDialog ad;
    private TextView titleView;
    private RelativeLayout messageView;
    private TextView sure;
    private TextView cancel;

    private CityPicker cityPicker;

    private LayoutInflater inflater;

    private boolean selected = false;

    public CityAlertDialog(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        inflater = LayoutInflater.from(context);
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_city);
        titleView = (TextView) window.findViewById(R.id.tv_dialog_base_title);
        messageView = (RelativeLayout) window.findViewById(R.id.rl_dialog_base_message);
        sure = (TextView) window.findViewById(R.id.tv_dialog_base_sure);
        cancel = (TextView) window.findViewById(R.id.tv_dialog_base_cancel);
        cityPicker = (CityPicker) window.findViewById(R.id.cp_city);
        cityPicker.setOnSelectingListener(new CityPicker.OnSelectingListener() {
            @Override
            public void selected(boolean selected1) {
                if (selected1) {
                    selected = true;
                }
            }
        });
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }

    /**
     * 设置内容视图
     *
     * @param resId
     */
    public void setMessage(View view) {
        messageView.addView(view);
    }

    /**
     * 获取选择的城市
     *
     * @return
     */
    public String getSelected() {
        if (selected) {
            return cityPicker.getCity_string();
        } else {
            return null;
        }
    }

    /**
     * 设置文本信息
     *
     * @param message
     */
    public void setMessage(String message) {
        TextView textView = new TextView(context);
        textView.setText(message);
        textView.setTextSize(18f);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(params);
        messageView.addView(textView);
    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text, final View.OnClickListener listener) {
        sure.setText(text);
        sure.setOnClickListener(listener);
    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text, final View.OnClickListener listener) {
        cancel.setText(text);
        cancel.setOnClickListener(listener);
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }

}